import { onMounted, ref } from "vue";
import { getSystemInfo } from "@/api/systemcofig";
import getURL from "@/utils/http/address";
import { useRouter } from "vue-router";
import { getTopMenu, initRouter } from "@/router/utils";
import { message } from "@/utils/message";
import router from "@/router";
import { useUserStoreHook } from "@/store/modules/user";
export function useSystemInfo() {
  const verison = ref();
  //图片地址

  const baseurl = ref(getURL);
  const logoImage = ref("");
  const leftimage = ref("");
  const systemId = ref("");
  const systemName = ref("");
  const bgColor = ref("");
  const fontColor = ref("");
  const isLogin = ref(false);
  async function getAllSystemInfo() {
    const res = await getSystemInfo();
    // console.log(res, "loging is res");
    if (res.result.sysVersion != null) {
      verison.value = res.result.sysVersion;
    }
    if (res.result.sysVersion != null) {
      logoImage.value = baseurl.value + "/wwwroot" + res.result.sysLog;
      leftimage.value = baseurl.value + "/wwwroot" + res.result.sysImg;
    }
    if (res.result.id != null) {
      systemId.value = res.result.id;
    }
    if (res.result.sysName != null) {
      systemName.value = res.result.sysName;
    }
    if (res.result.bgColor != null) {
      bgColor.value = res.result.bgColor;
    }
    if (res.result.fontColor != null) {
      fontColor.value = res.result.fontColor;
    }
  }
  async function getTicket() {
    const route = useRouter();
    const ticketss = route.currentRoute.value.query.ticket;
    //console.log("获取到的ticket" + ticketss);
    if (ticketss != null) {
      isLogin.value = true;
      const ticket = {
        ticket: ticketss
      };
      useUserStoreHook()
        .loginByYunHuaPage(ticket)
        .then(res => {
          if (res.success) {
            // 获取后端路由
            initRouter().then(() => {
              router.push(getTopMenu(true).path);
              //console.log(router.getRoutes(), "userLogin");
              message("登录成功", { type: "success" });
            });
            //使用静态路由
            // usePermissionStoreHook().handleWholeMenus([]);
            // addPathMatch();
            // router.push("/");
            // message("登录成功", { type: "success" });
          }
        });
    }
  }
  onMounted(() => {
    getAllSystemInfo();
    getTicket();
  });
  return {
    verison,
    logoImage,
    leftimage,
    systemId,
    systemName,
    getAllSystemInfo,
    getTicket,
    bgColor,
    fontColor,
    isLogin
  };
}
